//
//  ChanPinPeiZhiView.m
//  ShangChengMoBan
//
//  Created by 张铁军 on 16/9/22.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "ChanPinPeiZhiView.h"
#import "PeiZhiBtn.h"
#import "ChanPinYanSeModel.h"
#import "ShuXingXiangQing.h"
@implementation ChanPinPeiZhiView{
    UIButton *_jianBtn;
    
    UIButton *_addBtn;
    NSMutableArray *_zongBtnAry;
   
    UIColor *_choseColor;
    UIImageView * _shangPinimageView;
    UILabel * _prizeLabel;
    UILabel * _kuCunLabel;
    float _prize;
}


-(instancetype)initWithFrame:(CGRect)frame andChanPinmodel:(FL_ChanPinModel*)chanPin{
    self = [super initWithFrame:frame];
    if (self) {
        _chanPin = chanPin;
        [self initView];
    }
    return self;
}

-(void)initView{
    _choseColor = [UIColor redColor];
    _chonseBtnAry = [[NSMutableArray alloc]init];
    _zongBtnAry = [[NSMutableArray alloc]init];
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
   
   _contentView  = [[UIView alloc]initWithFrame:CGRectMake(0, 150*_kscH, KScreenWidth, 100*_kscH)];
    _contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_contentView];
    //商品图片
    _shangPinimageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, -10, 85*_kscH, 85*_kscH)];
    _shangPinimageView.image = [UIImage imageNamed:@"ii"];
  //  [_shangPinimageView sd_setImageWithURL:[NSURL URLWithString:_chanPin.coverImage]];
    [_contentView addSubview:_shangPinimageView];
    //关闭
    UIButton * guanBi = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-30, 10, 20, 20)];
    [guanBi setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [guanBi addTarget:self action:@selector(guanBi) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:guanBi];
    //价钱
    _prizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(_shangPinimageView.right+10, 14, 100, 15)];
    
    _prizeLabel.textColor = [UIColor redColor];
    _prizeLabel.font = [UIFont systemFontOfSize:13];
    _prizeLabel.text = [NSString stringWithFormat:@"¥%@",_chanPin.originalPrice];
    [_contentView addSubview:_prizeLabel];
    //库存
    _kuCunLabel = [[UILabel alloc]initWithFrame:CGRectMake(_shangPinimageView.right+10, _prizeLabel.bottom+3 , 200, 15)];
    _kuCunLabel.text= [NSString stringWithFormat:@"库存 %@件",_chanPin.focusCount];
    _kuCunLabel.font = [UIFont systemFontOfSize:13];
    [_contentView addSubview:_kuCunLabel];
    
    [self drawXunHuanShuXin];
}

-(void)guanBi{
    self.yv = KScreenHeight;
}

-(void)drawXunHuanShuXin{
    _myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _contentView.bottom , KScreenWidth, self.height - _contentView.bottom - 40)];
    
    _myScrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_myScrollView];
    
    _queDingBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, self.height - 40, KScreenWidth,40 )];
    [_queDingBtn addTarget:self action:@selector(queDingGouMaiBen) forControlEvents:UIControlEventTouchUpInside];
    [_queDingBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_queDingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _queDingBtn.backgroundColor = [UIColor orangeColor];
    [self addSubview:_queDingBtn];
    
    [self drawFenZuView];

}

-(void)drawFenZuView{
    
    int xiaofenLeiHang = 0;
    int titleNum = 0;
    int ZongHang = 0;
    float right = 0;
    
   // NSArray *allZongFenLEi = [_chanPin.goodsAttrMapper allKeys];
 
    for (int i = 0;  i < _chanPin.goodsConfig.count; i++) {
      //  NSString *key = [allZongFenLEi objectAtIndex:i];
        
     //   NSArray *itemAry = [_chanPin.goodsAttrMapper objectForKey:key];
        ChanPinYanSeModel * chan = _chanPin.goodsConfig[i];
        UILabel *tieleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * _kscW,(titleNum + ZongHang) * 35 * _kscH + 5 * _kscH, 300 * _kscW, 30 * _kscH)];
        tieleLabel.textColor = MaintextColor;
        tieleLabel.font = [UIFont systemFontOfSize:13];
        tieleLabel.text =  chan.name;
        [_myScrollView addSubview:tieleLabel];
        
        NSMutableArray *danXiangBtnAry = [[NSMutableArray alloc]init];
        
        for (int j = 0; j < chan.goodsConfigList.count; j++) {
            
         //   NSDictionary *itemIdeas = [itemAry objectAtIndex:j];
            ShuXingXiangQing * itemIdeas = chan.goodsConfigList[j];
            PeiZhiBtn *btn;
            btn = [[PeiZhiBtn alloc]initWithFrame:CGRectMake(right + 10 * _kscW, tieleLabel.bottom + 5* _kscH + xiaofenLeiHang * 35 , [self returnWidthWiht:itemIdeas.name], 25)];
//            [MainbtnAry addObject:btn];
            [btn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
            right = btn.right;
            btn._id = itemIdeas._id;
            btn.attrPrice = itemIdeas.configPrice;
            btn.goodsId = itemIdeas.goodsId;
            btn.attrValue = itemIdeas.name;
            btn.num = itemIdeas.stock;
            btn.tag = i * 100 + j;
            
            [btn setTitle:itemIdeas.name forState:UIControlStateNormal];

            if (j == 0) {
                btn.layer.borderColor = _choseColor.CGColor;
                [btn setTitleColor:_choseColor forState:UIControlStateNormal];
                [_chonseBtnAry addObject:btn];
            }else{
                btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
                [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            }
            
            if(j != chan.goodsConfigList.count - 1){
                if(right + [self returnWidthWiht:itemIdeas.name] > 300 * _kscW){//判断加入下一个会不会超出一行;
                    xiaofenLeiHang ++;
                    right = 0;
                    ZongHang++;
                }
            }
            [danXiangBtnAry addObject:btn];
            [_myScrollView addSubview:btn];
        }
        
        [_zongBtnAry addObject:danXiangBtnAry];
        ZongHang = ZongHang+1;
        xiaofenLeiHang = 0;
        titleNum++;
        right = 0;
        
        UILabel *fenGexianLabe = [[UILabel alloc]initWithFrame:CGRectMake(10 * _kscW, (titleNum + ZongHang) * 35 * _kscH + 5 * _kscH , 300 * _kscH, 1)];
        
        fenGexianLabe.backgroundColor = LIGHTGRAYBACKCOLOR;
        [_myScrollView addSubview:fenGexianLabe];

    }
    
    UIView *numberView = [[UIView alloc]initWithFrame:CGRectMake(0,(titleNum + ZongHang) * 35 * _kscH+  20,KScreenWidth, 50 * _kscH)];
    [_myScrollView addSubview:numberView];
    
    UILabel *numberTitle = [[UILabel alloc]initWithFrame:CGRectMake(10 * _kscW,10 * _kscH, 300 * _kscW, 30 * _kscH)];
    numberTitle.text = @"购买数量";
    numberTitle.textColor = MaintextColor;
    numberTitle.font = [UIFont systemFontOfSize:14];
    [numberView addSubview:numberTitle];
    
    _jianBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 130 * _kscW, 12 * _kscH, 40 * _kscW, 30 * _kscH)];
    _jianBtn.layer.borderWidth = 1;
    _jianBtn.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    [_jianBtn setTitle:@"-" forState:UIControlStateNormal];
    [_jianBtn setTitleColor:MaintextColor forState:UIControlStateNormal];
    [_jianBtn addTarget:self action:@selector(jianBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [numberView addSubview:_jianBtn];
    _numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(_jianBtn.right, _jianBtn.top, _jianBtn.width, _jianBtn.height)];
    _numberLabel.textAlignment = NSTextAlignmentCenter;
    _numberLabel.text = @"1";
    _numberLabel.textColor = MaintextColor;
    _numberLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _numberLabel.layer.borderWidth = 1;
    _numberLabel.font = [UIFont systemFontOfSize:13];
    [numberView addSubview:_numberLabel];
    
    _addBtn = [[UIButton alloc]initWithFrame:CGRectMake(_numberLabel.right, _jianBtn.top, _jianBtn.width, _jianBtn.height)];
    _addBtn.layer.borderWidth = 1;
    _addBtn.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    [_addBtn setTitle:@"+" forState:UIControlStateNormal];
    [_addBtn setTitleColor:MaintextColor forState:UIControlStateNormal];
    
    [_addBtn addTarget:self action:@selector(addBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [numberView addSubview:_addBtn];
    
    UILabel *fenGexianLabe = [[UILabel alloc]initWithFrame:CGRectMake(10 * _kscW, numberView.height - 1 , 300 * _kscH, 1)];
    fenGexianLabe.backgroundColor = LIGHTGRAYBACKCOLOR;
    [numberView addSubview:fenGexianLabe];
    
    _myScrollView.contentSize = CGSizeMake(KScreenWidth, numberView.bottom);
    if (_myScrollView.heightv > _myScrollView.contentSize.height) {
        _contentView.frame = CGRectMake(0, self.height-140*_kscH-numberView.bottom, KScreenWidth, 100*_kscH);
        _myScrollView.yv = _contentView.bottom;
    }
    [self jisuanJiaGe];
    
    
    
}

-(void)clicked:(PeiZhiBtn *)sender{
    
    NSArray *itemBtnary = [_zongBtnAry objectAtIndex:sender.tag/100];
    for (UIButton *btn in itemBtnary) {
        btn.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    
    UIButton *choseBtn =  [[_zongBtnAry objectAtIndex:sender.tag/100]objectAtIndex:(sender.tag%100)];
    choseBtn.layer.borderColor = [_choseColor CGColor];
    [choseBtn setTitleColor:_choseColor forState:UIControlStateNormal];
    
    [_chonseBtnAry replaceObjectAtIndex:sender.tag/100 withObject:choseBtn];
     _kuCunLabel.text= [NSString stringWithFormat:@"库存 %@件", sender.num];
    [self changeImageInfo:sender];
    
}

-(void)changeImageInfo:(PeiZhiBtn *)peiZhiBtn{
    
    for (ShuXingXiangQing *yanseModel in _chanPin.pictureFiles) {
        if ([yanseModel._id intValue] == [peiZhiBtn._id intValue]) {
            [_shangPinimageView sd_setImageWithURL:[NSURL URLWithString:yanseModel.url]];}

    };

    _shangPinimageView.image = [UIImage imageNamed:@"ii"];
    
    [self jisuanJiaGe];
}

-(void)jisuanJiaGe{
    int otherjiaGe = 0;
    for (int i = 0;i < _chonseBtnAry.count ; i ++) {
        PeiZhiBtn *peiZhiBtn = [_chonseBtnAry objectAtIndex:i];
        otherjiaGe += [peiZhiBtn.attrPrice floatValue];
    }
    _prize = otherjiaGe + [_chanPin.ourPrice floatValue];
//    int a = [_numberLabel.text intValue];
    _prizeLabel.text = [NSString stringWithFormat:@"￥%.1f",_prize];
}

-(float)returnWidthWiht:(NSString *)title{
    
    NSDictionary *attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:13]};
    CGSize maxSize = CGSizeMake(1000, 20);
    float width = [title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width+10;
    
    return width;
}

//减号
-(void)jianBtnClicked:(UIButton *)sender{
    int a = [_numberLabel.text intValue];
    a--;
    if (a >0) {
        _numberLabel.text = [NSString stringWithFormat:@"%d",a];
        // _prizeLabel.text = [NSString stringWithFormat:@"￥%.2f",_prize*a];
    }
   
}
//加号
-(void)addBtnClicked:(UIButton *)sender
{
    int a = [_numberLabel.text intValue];
    a++;
    _numberLabel.text = [NSString stringWithFormat:@"%d",a];
   //  _prizeLabel.text = [NSString stringWithFormat:@"￥%.2f",_prize*a];
    
}

-(void)queDingGouMaiBen{
    [self.delegate quDingBtnWiht:_chonseBtnAry and:_numberLabel.text];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
